package Dao.Student.Imp;

import Dao.Student.StudentDao;
import Entity.Grade.Score;
import Entity.Process.StageTime;
import Entity.Student.StuInfo;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImp implements StudentDao {
    private ResultSet rs;
    private PreparedStatement ps;


    /**
     * 根据sno查询学生信息
     *
     * @param sno
     * @param conn
     * @return
     */
    @Override
    public StuInfo getStuInfo(String sno, Connection conn) {
        StuInfo stu = new StuInfo();
        String sql = "select s.sno,s.name,s.collname,s.classes,s.major_name,t.name,xt.title,xt.is_pass from stu_info s,teacher t,title xt where s.sno = xt.no and xt.tno = t.tno and s.sno = ?;";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1, sno);
            rs = ps.executeQuery();
            while (rs.next()) {
                stu.setSno(rs.getString(1));
                stu.setSname(rs.getString(2));
                stu.setCollName(rs.getString(3));
                stu.setClasses(rs.getString(4));
                stu.setMajorName(rs.getString(5));
                stu.setTname(rs.getString(6));
                stu.setTitle(rs.getString(7));
                stu.setIsPass(rs.getString(8));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeMySQLResource(ps,rs);
        }
        return stu;
    }

    /**
     * 根据sno查询评分
     *
     * @param sno
     * @param conn
     * @return
     */
    @Override
    public List<Score> getStuScore(String sno, Connection conn) {
        List<Score> scores = new ArrayList<>();
        String sql = "select score ,satge \n" +
                "from score\n" +
                "where sno = ? GROUP BY satge";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,sno);
            rs = ps.executeQuery();
            while(rs.next()){
                Score score = new Score();
                score.setScore(rs.getString("score"));
                score.setSatge(rs.getString("satge"));
                scores.add(score);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.closeMySQLResource(ps,rs);
        }

        return scores;
    }

    /**
     * 根据sno查询答辩信息
     *
     * @param sno
     * @param conn
     * @return
     */
    @Override
    public StuInfo getStuDefence(String sno, Connection conn) {
        return null;
    }

    /**
     * 根据sno查询进度表
     *
     * @param sno
     * @param conn
     * @return
     */
    @Override
    public List<StageTime> getStuSchedule(String sno, Connection conn) {
        List<StageTime> stageTimes = new ArrayList<>();
        String sql = "";
        return null;
    }
}
